package me.xuym.md2htm;

import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html.IndependentLinkResolverFactory;
import com.vladsch.flexmark.html.LinkResolver;
import com.vladsch.flexmark.html.renderer.LinkResolverContext;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;
import me.xuym.md2htm.extension.GitlabLinkResolver;

/**
 * Created by broche on 11/12/2017.
 */
public class FlexmarkTranslator {
    private Parser parser;
    private HtmlRenderer renderer;

    public FlexmarkTranslator() {
        MutableDataSet options = new MutableDataSet();
        parser = Parser.builder(options).build();
        renderer = HtmlRenderer.builder(options).linkResolverFactory(new MyLinkResolverFactory()).build();
    }

    public String translate(String content) {
        Node document = parser.parse(content);
        return renderer.render(document);
    }

    static class MyLinkResolverFactory extends IndependentLinkResolverFactory {
        private GitlabLinkResolver linkResolver = new GitlabLinkResolver();

        @Override
        public LinkResolver create(LinkResolverContext context) {
            return linkResolver;
        }
    }
}
